<template>
  <a-modal
    title="修改用户"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="!addable"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['userId']"
            :disabled="editable || showable"
          />
        </a-form-item>
        <a-form-item
          label="用户名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="editable || showable"
            v-decorator="['username', {rules: [{required: true, message: '请输入用户名'}]}]" />
        </a-form-item>
        <a-form-item
          label="密码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            type="password"
            v-decorator="['password']" />
        </a-form-item>
        <a-form-item
          label="部门"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-tree-select
            :disabled="showable"
            showSearch
            placeholder="选择部门"
            allowClear
            :treeData="treeData"
            treeDefaultExpandAll
            treeNodeFilterProp="title"
            v-decorator="['deptId', {rules: [{required: true, message: '请选择部门'}],initialValue: '1'}]"
          />
        </a-form-item>
        <a-form-item
          label="状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            v-decorator="['status', {rules: [{required: true, message: '选择状态'}],initialValue: 1}]"
            :options="statusList"
          >
          </a-select>
        </a-form-item>
        <a-form-item
          label="角色"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            :disabled="showable"
            mode="multiple"
            v-decorator="['roleIdList', {rules: [{required: true, message: '选择状态'}]}]"
            :options="roleList"
          >
          </a-select>
        </a-form-item>
        <a-form-item
          label="手机号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['phone', {rules: [{required: true, message: '请输入手机号'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer" >
      <div
        v-show="editable || addable"
      >
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>

</template>

<script>
import pick from 'lodash.pick'
import { getObj, addObj, putObj } from '@api/sys/user'
import { roleDropDown } from '@api/sys/role'
import { treeDataTranslate } from '@/utils/util'
import { fetchList } from '@api/sys/dept'
export default {
  name: 'UserAddOrUpdate',
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      editable: false,
      addable: false,
      showable: false,
      treeData: [],
      statusList: [
        {
          label: '禁用',
          value: 0
        },
        {
          label: '正常',
          value: 1
        }
      ],
      roleList: [],
      type: 'add'
    }
  },
  methods: {
    // 获取用户信息
    edit (id, type) {
      this.visible = true
      if (type && type === 'add') {
        this.addable = true
        this.type = type
      }
      if (type === 'edit') {
        this.editable = true
        this.type = type
      }
      if (type === 'show') {
        this.showable = true
        this.type = type
      }
      roleDropDown().then(res => {
        this.roleList = res.data
      })
      fetchList().then(res => {
        this.treeData = treeDataTranslate(res.data, 'deptId', 'name')
      })
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data.sysUser
          record.roleIdList = res.data.roles
          record.deptId = String(record.deptId)
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['userId', 'username', 'phone', 'status', 'deptId', 'roleIdList']))
          })
        })
      } else {
        resetFields()
      }
    },
    // 结束保存用户信息
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          if (this.type === 'add') {
            await addObj(values)
          } else if (this.type === 'edit') {
            await putObj(values)
          }
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('ok')
            this.visible = false
          }, 1500)
        }
      })
    },
    handleCancel () {
      this.visible = false
      setTimeout(() => {
        this.addable = false
        this.showable = false
        this.editable = false
      }, 200)
    }
  }
}
</script>

<style scoped>

</style>
